<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Cache;

class Base extends \app\base\controller\Base
{
	public function _initialize()
    {
    	//菜单 
    	/* $menus = Cache::remember('menus',function(){
			$list = Db::name('menu')->where('state',1)->select();
			$menu = Arr2Tree($list);
			return $menu;
		}); */
		$list = Db::name('menu')->where('state',1)->select();
		$menus = Arr2Tree($list);
		$this->isLogin();
		
		if($this->user['user_id'] == 6){
			$menus = array_slice($menus,0,2);
		}
    	
		$this->assign('menus', $menus);
		$this->assign('menu_current', strtolower('/'.request()->module().'/'.request()->controller())  );
		$this->assign('static_dir', '');
		$this->assign('notPopup', 1);
		$this->assign('list', [ ['test'=>'testvalue'] ]);
		
    }
    
    //列表
    public function index() {
		
		$where = $this->search();
		
		//列表
		$list = $this->joins()->where($where)->whereNull('m.delete_time')->fetchSql(0)->paginate(10);
		
		$this->assign('list',$list);
		$this->assign('pager',$list->render());
		
		return $this->out();
		
	}
	
	//连接共用
	protected function joins() {
		
		$Query = Db::name($this->table)->alias('m')->field($this->field);
		if($this->join || $this->joins) {
			
			$joins = $this->joins ?: [$this->join];
			foreach($joins AS $join) {
				$Query = $Query->join($join[0],$join[1],$join[2]);
			}
		}
		
		return $Query;
	}
	
	//搜索
	protected function search() {
		//搜索
		$where = [];
		$P = input('param.');
		if($P) {
			foreach($P AS $key=>$val) {
				
				if( $val && !in_array($key, ['op','page']) ){
					if($key == 'start_date'){
						$where['m.'.$key] = ['>=',$val];
					} elseif($key =='end_date'){
						$where['m.'.$key] = ['<=',$val];
					} elseif( in_array($key, ['name','phone']) ) {
						$where['m.'.$key] = ['LIKE','%'.$val.'%'];
					} elseif( StrFind($key,'_time') ) {
						$where['m.'.$key] = $val;
					} else {
						$where['m.'.$key] = $val;
					}
				}
			}
		}
		$this->options($where);
		
		//弹出层搜索
		$op = input('param.op');
		if(in_array($op, ['search','bind','unbind']) ) {
			$this->assign('notPopup',0);
		}
		$this->assign('op',$op);
		
		return $where;
		
	}
    
    public function detail() {
		
		$item = $this->joins()->find();
		
		$this->assign('item',$item);
		return $this->out();
		
	}
	
	//添加
	public function add() {
		
		if(request()->isPost()) {
			
			$data = $this->data();
			$id = Db::name($this->table)->insertGetId($data);
			
			//更新缓存
			return $this->res($rslt,[ 'msg' => '添加成功','data'=>$id],'index');
		}
		//选项 
		$this->options();
		
		return $this->out();
		
	}
	
	public function edit() {
		
		$id = input('param.'.$this->id.'/d');
		
		if(!$id) {
			return $this->error('入口非法，请重试');
		}
		//提交
		if(request()->isPost()) {
			
			$data = $this->data();
			$rslt = Db::name($this->table)->where($this->id,$id)->update($data);
				
			return $this->res($rslt,['修改并保存成功','内容未修改或网络问题，请联系开发者 i@fazi.me'],1);
			
		}
		//连接查询
		$item = $this->joins()->where($this->id,$id)->find();
		
		//选项
		$item = $this->options($item);
		$this->assign('item',$item);
		
		return $this->out('add');
		
	}
	
	//删除 
	public function delete() {
		
		$id = input('post.id/d');
		
		if($id) {
			$rslt = Db::name($this->table)->where($this->id,$id)->setField('delete_time',time());
		}
		
		return $this->res($rslt);
		
	}
	
	//状态 
	public function state() {
		
		$id = input('post.id/d');
		$state = input('post.state/d');
		
		if($id) {
			$rslt = Db::name($this->table)->where($this->id,$id)->setField('state',$state);
		}
		
		return $this->res($rslt);
		
	}
	
	
	//上传
	protected function upload($data){
		
    	// 获取表单上传文件
	    $files = request()->file();
	    if($files){
		    foreach($files as $name => $file){
		        // 移动到框架应用根目录/public/uploads/ 目录下
		        $info = $file->move(UPLOAD_PATH);
		        if($info){
		            // 成功上传后 获取上传信息
		            // 输出 42a79759f284b767dfcb2a0197904287.jpg
		            $data[$name] = $info->getSaveName(); 
		        }else{
		            // 上传失败获取错误信息
	//	            echo $file->getError();
		        }    
		    }
	    }
	    return $data;
	}
	
	//更新缓存
	protected function clearcache($rslt) {
		if($rslt) Cache::rm($this->table);
	}
	
	protected function data() {
		$P = input('post.');
		
		//ID
		if( isset($P['id']) ) {
			$P[$this->id] = $P['id'];
			unset($P['id']);
		}
		//数据整理
		$data = $this->parseData( array_map('Arr2Set',$P) );
		//数组值变SET
		if($data[$this->id]) {
			
		} else {
			unset($data[$this->id]);
		}
		//上传图片
		$data = $this->upload($data);
		
		return $data;
	}
	
	protected function parseData($data = []) {
		return $data;
	}
	
	protected function options($item = []) {
		return $item;
	}
	
}
